#!/usr/bin/env python3
"""
MCP Greeting & Calculator Server
一个提供打招呼和计算器功能的MCP服务器
"""

import math
from datetime import datetime
from typing import Any, Dict

# MCP相关导入
try:
    from mcp.server.fastmcp import FastMCP
except ImportError:
    print("请安装mcp包: pip install mcp")
    exit(1)

# 创建服务器实例
mcp = FastMCP("greeting-calculator-server")

# 用户个性化设置存储
user_preferences = {
    "name": "朋友",
    "greeting_style": "friendly",
    "language": "zh"
}

# 打招呼样式模板
greeting_templates = {
    "friendly": {
        "zh": {
            "morning": "早上好，{name}！希望你今天过得愉快！",
            "afternoon": "下午好，{name}！今天过得怎么样？",
            "evening": "晚上好，{name}！辛苦了一天！",
            "general": "你好，{name}！很高兴见到你！"
        },
        "en": {
            "morning": "Good morning, {name}! Have a wonderful day!",
            "afternoon": "Good afternoon, {name}! How's your day going?",
            "evening": "Good evening, {name}! You've had a long day!",
            "general": "Hello, {name}! Nice to see you!"
        }
    },
    "formal": {
        "zh": {
            "morning": "早上好，{name}先生/女士。",
            "afternoon": "下午好，{name}先生/女士。",
            "evening": "晚上好，{name}先生/女士。",
            "general": "您好，{name}先生/女士。"
        },
        "en": {
            "morning": "Good morning, Mr./Ms. {name}.",
            "afternoon": "Good afternoon, Mr./Ms. {name}.",
            "evening": "Good evening, Mr./Ms. {name}.",
            "general": "Hello, Mr./Ms. {name}."
        }
    },
    "casual": {
        "zh": {
            "morning": "早啊，{name}！",
            "afternoon": "嗨，{name}！",
            "evening": "晚上好，{name}！",
            "general": "嘿，{name}！"
        },
        "en": {
            "morning": "Morning, {name}!",
            "afternoon": "Hey, {name}!",
            "evening": "Evening, {name}!",
            "general": "Hey there, {name}!"
        }
    }
}

# 告别消息模板
farewell_templates = {
    "friendly": {
        "zh": "再见，{name}！期待下次见面！",
        "en": "Goodbye, {name}! Looking forward to seeing you again!"
    },
    "formal": {
        "zh": "再见，{name}先生/女士。",
        "en": "Goodbye, Mr./Ms. {name}."
    },
    "casual": {
        "zh": "拜拜，{name}！",
        "en": "See ya, {name}!"
    }
}

def get_time_period():
    """根据当前时间返回时间段"""
    hour = datetime.now().hour
    if 5 <= hour < 12:
        return "morning"
    elif 12 <= hour < 18:
        return "afternoon"
    elif 18 <= hour < 22:
        return "evening"
    else:
        return "general"

@mcp.tool()
def greet(name: str = "朋友", style: str = "friendly", language: str = "zh") -> str:
    """个性化打招呼功能
    
    Args:
        name: 要打招呼的人的名字
        style: 打招呼的风格 (friendly/formal/casual)
        language: 语言选择 (zh/en)
    """
    # 验证参数
    if style not in ["friendly", "formal", "casual"]:
        style = user_preferences["greeting_style"]
    if language not in ["zh", "en"]:
        language = user_preferences["language"]
    
    time_period = get_time_period()
    template = greeting_templates[style][language][time_period]
    message = template.format(name=name)
    return message

@mcp.tool()
def farewell(name: str = "朋友", style: str = "friendly", language: str = "zh") -> str:
    """个性化告别功能
    
    Args:
        name: 要告别的人的名字
        style: 告别的风格 (friendly/formal/casual)
        language: 语言选择 (zh/en)
    """
    # 验证参数
    if style not in ["friendly", "formal", "casual"]:
        style = user_preferences["greeting_style"]
    if language not in ["zh", "en"]:
        language = user_preferences["language"]
    
    template = farewell_templates[style][language]
    message = template.format(name=name)
    return message

@mcp.tool()
def add(a: float, b: float) -> str:
    """加法运算
    
    Args:
        a: 第一个数
        b: 第二个数
    """
    result = a + b
    return f"{a} + {b} = {result}"

@mcp.tool()
def subtract(a: float, b: float) -> str:
    """减法运算
    
    Args:
        a: 被减数
        b: 减数
    """
    result = a - b
    return f"{a} - {b} = {result}"

@mcp.tool()
def multiply(a: float, b: float) -> str:
    """乘法运算
    
    Args:
        a: 第一个数
        b: 第二个数
    """
    result = a * b
    return f"{a} × {b} = {result}"

@mcp.tool()
def divide(a: float, b: float) -> str:
    """除法运算
    
    Args:
        a: 被除数
        b: 除数
    """
    if b == 0:
        return "错误：除数不能为零！"
    result = a / b
    return f"{a} ÷ {b} = {result}"

@mcp.tool()
def square(number: float) -> str:
    """平方运算
    
    Args:
        number: 要计算平方的数
    """
    result = number ** 2
    return f"{number}² = {result}"

@mcp.tool()
def sqrt(number: float) -> str:
    """开根号运算
    
    Args:
        number: 要开根号的数
    """
    if number < 0:
        return "错误：不能计算负数的平方根！"
    result = math.sqrt(number)
    return f"√{number} = {result}"

@mcp.tool()
def set_preferences(name: str = None, greeting_style: str = None, language: str = None) -> str:
    """设置用户个性化偏好
    
    Args:
        name: 用户名字
        greeting_style: 默认打招呼风格 (friendly/formal/casual)
        language: 默认语言 (zh/en)
    """
    if name is not None:
        user_preferences["name"] = name
    if greeting_style is not None and greeting_style in ["friendly", "formal", "casual"]:
        user_preferences["greeting_style"] = greeting_style
    if language is not None and language in ["zh", "en"]:
        user_preferences["language"] = language
    
    return f"偏好设置已更新：{user_preferences}"

if __name__ == "__main__":
    mcp.run()